
package de.fmannan.addbook;


import org.apache.log4j.Logger;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;

/**
 * @author fmannan
 * 
 * This class controls all aspects of the application's execution. It's the
 * entry point for the application when its being run - just like the main
 * method serves as the entry point. This class must be linked into the Eclipse
 * Runtime'sapplications extension point thus making the runtime aware of the
 * application
 * 
 */
public class Application implements IApplication {

	private static Logger log = Logger.getLogger(Application.class);
	/**
	 * Is executed on application startup.
	 */
	public Object start(IApplicationContext context) throws Exception 
	{
		log.debug("Starting application");
		// The application creates a display...
		Display display = PlatformUI.createDisplay();
		MyImageRegistry.loadImages(display);		
			
		try {
			// ... and then starts the eclipse workbench, which causes a window
			// to be opened and loops forever while waiting for events such as
			// mouse-clicks
			int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
			if (returnCode == PlatformUI.RETURN_RESTART)
				return IApplication.EXIT_RESTART;
			else
				return IApplication.EXIT_OK;
		} finally {
			display.dispose();
		}
		
	}


	/**
	 * Is called when the application is exited.
	 */
	public void stop() {	
		log.debug("Stopping application");
		final IWorkbench workbench = PlatformUI.getWorkbench();
		if (workbench == null)
			return;
		final Display display = workbench.getDisplay();
		display.syncExec(new Runnable() {
			public void run() {
				if (!display.isDisposed())
					workbench.close();
			}
		});
	}
	
}
